package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;


    @PostMapping
    public R add(@RequestBody DishDto dishDto) {
        // System.out.println("dishDto = " + dishDto);

        int add = dishService.add(dishDto);
        if (add > 0) {
            return R.success("成功");
        }

        return R.error("失败");
    }

    @GetMapping("/page")
    public R findPage(PageParam pageParam) {
        IPage<DishDto> page = dishService.findPage(pageParam);

        return R.success(page);
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        int row = dishService.update(dishDto);
        if (row > 0) {
            return R.success("跟新成功");
        }
        return R.error("跟新失败");
    }

    /**
     * 根据分类id查询菜品数据
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(long categoryId, Integer status) {
        List<DishDto> dishList = dishService.findByCategoryId(categoryId, null);
        return R.success(dishList);
    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        int delete = dishService.delete(ids);
        if (delete > 0) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping("/status/{status}")
    public R updateByState(@PathVariable int status, @RequestParam List<Long> ids) {
        dishService.updateByState(status, ids);

        return R.success("批量停售成功");

        // return R.error("批量停售失败");

    }
}